package juc.threadlocal;

/**
 * @author fangbin
 * @desc
 * @date 2020/5/20 21:15
 */
public class ThreadLocalTransferParams {
    public static void main(String[] args) {

       for(int i=0;i<10;i++){
           int finalI = i;
           new Thread(new Runnable() {
               @Override
               public void run() {
                   new Service1().process("param"+ finalI);
               }
           }).start();
       }
    }
}

class UserContextHolder{

    public static ThreadLocal<User> threadLocal = new ThreadLocal();
}
class Service1{
    public void process(String param){
        User user = new User(param);
        UserContextHolder.threadLocal.set(user);
        new Service2().process();
    }

}
class Service2{
    public void process(){
        User user =  UserContextHolder.threadLocal.get();
        System.out.println(Thread.currentThread().getName()+":"+user);
        new Service3().process();

    }
}

class Service3{
    public void process(){
        User user =  UserContextHolder.threadLocal.get();
        System.out.println(Thread.currentThread().getName()+":"+user);
    }
}


class User{
    private String username;

    public User(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                '}';
    }
}